//
//  HP_LoanViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/8/30.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "HP_LoanViewController.h"
#import "HP_HotTitleTableViewCell.h"
#import "HP_LoanTableViewCell.h"
#import "HP_LoanHeaderView.h"
#import "Mediator+Loan.h"
#import "HomePageViewController.h"
#import "HP_LoanListViewController.h"
#import "HomePageRequestTools.h"
#import "Loan_h5WebViewController.h"

@interface HP_LoanViewController ()<UITableViewDelegate,UITableViewDataSource,HP_HotTitleTableViewCellDelegate,HP_LoanTableViewCellDelegate>

@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)HP_LoanHeaderView *headerView;


@property(nonatomic,assign)NSInteger pageNum;//页码数
@property(nonatomic,assign)NSInteger pageSize;//每页数量
@property(nonatomic,strong)NSString *keyword;//keyword = 1 返回推荐产品，keyword = 0 返回非推荐产品，不传，返回混合的。
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSString *total;


@end

@implementation HP_LoanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.pageSize = 10;
    self.pageNum = 1;
    self.keyword = @"1";
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.myTableView.frame = self.view.frame;
    self.myTableView.backgroundColor = ListMaoBackgroundColor;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[HP_HotTitleTableViewCell class] forCellReuseIdentifier:@"HP_HotTitleTableViewCell"];
    [self.myTableView registerClass:[HP_LoanTableViewCell class] forCellReuseIdentifier:@"HP_LoanTableViewCell"];
    
    self.headerView = [[HP_LoanHeaderView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 155*ScreenWidthMao/375.f)];
    self.myTableView.tableHeaderView = self.headerView;
    self.myTableView.contentInset = UIEdgeInsetsMake(0, 0, 64+49, 0);
    [self.view addSubview:self.myTableView];
    [self.myTableView reloadData];
    
    
    HFQMJRefreshHeader *header = [HFQMJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getListData)];
    self.myTableView.mj_header = header;
    self.myTableView.mj_footer = [HFQMJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(updateMoreHistoryCodeInfo)];
    
    [self updateListData];
    
}

-(void)getListData{
    self.pageSize = 10;
    self.pageNum = 1;
    [self.dataArr removeAllObjects];
    [self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    [self updateListData];
}

-(void)updateMoreHistoryCodeInfo{
    
    if (self.dataArr.count < [self.total integerValue]) {
        self.pageNum = self.pageNum +1;
        [self updateListData];
    }else{
        [self.myTableView.mj_footer endRefreshingWithNoMoreData];
    }
    
}

- (void)updateListData {
    
    NSDictionary *param_dic = @{@"pageNum":@(self.pageNum),
                                @"pageSize":@(self.pageSize),
                                @"keyword":self.keyword
                                
                                };
    
    NSLog(@"----参数-----%@",param_dic);
    [HFQIndicationView showInView:self.view];
    __weak typeof (self)weakSelf = self;
    [HomePageRequestTools LoanProductListDataWithParameters:param_dic Success:^(id  _Nullable responseObject) {
        
        [weakSelf.myTableView.mj_header endRefreshing];
        [weakSelf.myTableView.mj_footer endRefreshing];
        [HFQIndicationView dismiss];
        
        if ([[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] isEqualToString:@"200"]) {
            
            NSArray *itemArray = responseObject[@"data"][@"rows"];
            self.total = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"total"]];
            for (NSDictionary *dic in itemArray) {
                LoanModel *model = [[LoanModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArr addObject:model];
            }
            
            [self.myTableView reloadData];
        }
        
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        [weakSelf.myTableView.mj_header endRefreshing];
        [weakSelf.myTableView.mj_footer endRefreshing];
        [HFQIndicationView dismiss];
    }];
}

-(void)pushH5Action:(LoanModel *)model{
    
    Loan_h5WebViewController *h5 = [[Loan_h5WebViewController alloc]init];
    h5.url = model.app_h5_link;
    HomePageViewController *mainVC = (HomePageViewController *)self.nextResponder.nextResponder.nextResponder;
    [mainVC.navigationController pushViewController:h5 animated:YES];
}

-(void)moreBtnAction{
    
    HP_LoanListViewController *listVC = [[HP_LoanListViewController alloc]init];
    listVC.view.backgroundColor = [UIColor whiteColor];
    HomePageViewController *mainVC = (HomePageViewController *)self.nextResponder.nextResponder.nextResponder;
    [mainVC.navigationController pushViewController:listVC animated:YES];
}
#pragma mark ---------tableViewDelegate---------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        HP_HotTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HP_HotTitleTableViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        HP_LoanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HP_LoanTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        
        if (self.dataArr.count != 0) {
            LoanModel *model = self.dataArr[indexPath.section-1];
            [cell updateUIWithModel:model];
        }
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 40;
    }
    return 122;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = ListMaoBackgroundColor;
    return view;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = ListMaoBackgroundColor;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section != 0) {
       
    //h5页面跳转
        
    }
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
